<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录日志</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格顶部工具栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="username" class="layui-input" type="text" placeholder="请输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0" style="min-width: 200px;">
                            <input id="edtDate" class="layui-input date-icon" type="text" placeholder="请选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="searchSubmit" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnExport" type="button" class="layui-btn icon-btn" lay-tips="支持搜索后导出">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="logTable" lay-filter="logTable"></table>
        </div>
    </div>
</div>

<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var lastWhere;  // 搜索条件，用于导出功能

        // 渲染表格
        var insTb = tableX.render({
            elem: '#logTable',
            url: 'loginRecord/loginRecordList',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'username', title: '账号', sort: true},
                {field: 'nickName', title: '用户名', sort: true},
                {field: 'ipAddress', title: 'IP', sort: true},
                {field: 'device', title: '设备', sort: true},
                {field: 'osName', title: '设备类型', sort: true},
                {field: 'browserType', title: '浏览器', sort: true},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '登录时间'
                }
            ]]
        });

        // 时间范围
        laydate.render({
            elem: '#edtDate',
            type: 'date',
            range: true
        });

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            data.field.startDate = '';
            data.field.endDate = '';
            var selDate = $('#edtDate').val().split(' - ');
            selDate[0] && (data.field.date1 = selDate[0]);
            selDate[1] && (data.field.date2 = selDate[1]);
            lastWhere = data.field;
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 导出数据
        $('#btnExport').click(function () {
            tableX.exportDataBack('loginRecord/export', lastWhere, 'get');
        });

    });
</script>
</body>
</html>